#!/bin/sh /etc/rc.common

START=97

if type extra_command >/dev/null; then
	extra_command "rclone_installed" " Get Rclone installation"
else
	EXTRA_COMMANDS="${EXTRA_COMMANDS} rclone_installed"
fi

cifs_header() {
	local enabled
	local delay
	local cache_dir
	local rclone_cache_mode rclone_extras

	config_get enabled $1 enabled
	config_get delay $1 delay 0
	config_get cache_dir $1 cache_dir "/tmp/cache/davfs"
	config_get rclone_cache_mode $1 rclone_cache_mode
	config_get rclone_extras $1 rclone_extras

	ENABLED=$enabled
	DELAY=$delay
	CACHE_DIR="$cache_dir"
	if [ -n "$rclone_cache_mode" -a "$rclone_cache_mode" != "off" ]; then
		rclone_extras="--cache-dir \"$cache_dir/rclone\" --vfs-cache-mode $rclone_cache_mode $rclone_extras"
	fi
	RCLONE_EXTRAS="$rclone_extras"
}

mount_natshare() {
	local server
	local name
	local natpath
	local guest
	local users
	local pwd
	local agm
	local iocharset
	local smbver
	local enabled

	config_get enabled $1 enabled 1
	[ "$enabled" = 1 ] || return 0
	config_get server $1 server
	config_get name $1 name
	config_get natpath $1 natpath
	config_get guest $1 guest
	config_get users $1 users
	config_get pwd $1 pwd
	config_get agm $1 agm "ro"
	config_get iocharset $1 iocharset
	config_get smbver $1 smbver

	if [ -z "$server" -o -z "$name" -o -z "$natpath" ]; then
		logger -t cifs "server/name or mountpoint is empty"
		return 1
	fi
	mountpoint -q "$natpath" && return 0
	mkdir -p "$natpath" && chmod 777 "$natpath"

	if [ "$smbver" = "nil" ];then
		smbverarg=""
	else
		smbverarg="vers=$smbver,"
	fi
	#echo "mount -t cifs -o ${smbverarg}username=$users,password=$pwd,iocharset=$iocharset,$agm //$server/$name $natpath"
	/usr/bin/mount -t cifs -o "${smbverarg}username=$users,password=$pwd,iocharset=$iocharset,$agm" "//$server/$name" "$natpath"
}

mount_davfs() {
	local url
	local username
	local password
	local mountpoint
	local arguments
	local engine
	local enabled
	local rclone_mount_args

	config_get enabled $1 enabled 1
	[ "$enabled" = 1 ] || return 0
	config_get url $1 url
	config_get username $1 username
	config_get password $1 password
	config_get mountpoint $1 mountpoint
	config_get arguments $1 arguments
	config_get engine $1 engine 'auto'

	if [ -z "$url" -o -z "$mountpoint" ]; then
		logger -t davfs "url or mountpoint is empty"
		return 1
	fi
	mountpoint -q "$mountpoint" && return 0
	mkdir -p "$mountpoint" && chmod 777 "$mountpoint"

	if [ -n "$RCLONE_INSTALLED" -a "$engine" = "auto" ]; then
		engine=rclone
	fi

	if [ "$engine" = "rclone" ]; then
		touch /var/etc/cifs-mount/rclone_$1.conf
		chmod 600 /var/etc/cifs-mount/rclone_$1.conf
		rclone --config /var/etc/cifs-mount/rclone_$1.conf config create --non-interactive \
			cifs_rclone_$1 webdav "user=$username" "pass=$password" "url=$url" || return 1
		rclone_mount_args="--allow-other --default-permissions"
		echo "$arguments" | grep -Fwq ro && rclone_mount_args="$rclone_mount_args --read-only"
		eval "rclone --config /var/etc/cifs-mount/rclone_$1.conf mount --daemon $RCLONE_EXTRAS $rclone_mount_args \
			--devname \"$url\" cifs_rclone_$1: \"$mountpoint\""
		return $?
	fi

	[ -n "$arguments" ] && arguments=",$arguments"

	if [ "$engine" = "webdavfs" ]; then
		arguments="allow_other$arguments"
		[ -n "$password" ] && arguments="password=$password,$arguments"
		[ -n "$username" ] && arguments="username=$username,$arguments"
		/usr/bin/mount -t webdavfs -o "$arguments" "$url" "$mountpoint"
		return $?
	fi
	{ grep -v '^cache_dir ' /etc/davfs2/script.conf ; echo; echo "cache_dir $CACHE_DIR/davfs2"; } >/var/etc/cifs-mount/davfs2.conf
	# echo "/usr/bin/mount -t davfs -o \"conf=/var/etc/cifs-mount/davfs2.conf$arguments\" \"$url\" \"$mountpoint\""
	{ echo "$username"; echo "$password"; } | /usr/bin/mount -t davfs -o "conf=/var/etc/cifs-mount/davfs2.conf$arguments" "$url" "$mountpoint"
}

boot() {
	BOOT=1
	start
}

start() {
	ENABLED=0
	DELAY=0
	CACHE_DIR=/tmp/cache/davfs
	RCLONE_EXTRAS=

	config_load cifs
	config_foreach cifs_header cifs

	if [ "$ENABLED" == 1 ]; then
		if [ "$BOOT" == 1 -a "$DELAY" -gt 0 ]; then
			sleep "$DELAY"
		fi
		config_foreach mount_natshare natshare
		#echo "Cifs Mount succeed."
		if rclone_installed >/dev/null; then
			RCLONE_INSTALLED=1
		fi
		rm -rf /var/etc/cifs-mount
		mkdir -p /var/etc/cifs-mount
		mkdir -p "$CACHE_DIR/davfs2" "$CACHE_DIR/rclone"
		chmod 0777 "$CACHE_DIR/davfs2"
		chown nobody:nogroup "$CACHE_DIR/davfs2"
		chmod 0700 "$CACHE_DIR/rclone"
		config_foreach mount_davfs davfs
	fi
}

stop() {
	/usr/bin/mount | grep '//' | awk -F ' ' '{print $3}' | while read line; do
		#echo "umount -d -l $line"
		/usr/bin/umount -d -l $line 2>/dev/null
	done
	rm -rf /var/etc/cifs-mount
}

rclone_installed() {
	local version=$(rclone version 2>/dev/null |head -1 |cut -d' ' -f2)
	[ -z "$version" ] && return 1
	echo "$version"
}
